package com.jzoom.zoom.admin.daos;

import com.jzoom.zoom.admin.models.AdminDaoModel;
import com.jzoom.zoom.admin.models.ContentSercie;
import com.jzoom.zoom.common.annotations.Inject;
import com.jzoom.zoom.dao.Record;
import com.jzoom.zoom.web.exception.StatusException;

import java.util.Map;

public class PageDao extends AdminDaoModel {
	
	@Inject
	private ContentSercie contentSercie;

	public PageDao() {
        super("sys", "sys_page", "id");
	}
	
	
	public int put(String id, Map<String, Object> data) {
		Record record = dao.table("sys_page").where("id", id).get();
		if(record==null) {
			throw new StatusException.ApiError("页面不存在");
		}
        int ret = this.getPrimaryKeyAdapter().where(dao.table(table), id).setAll(data).update();
		updateCache(record);
		return ret;
	}
	
	private void updateCache(Record record) {
		String url = record.getString("url");
		contentSercie.updatePageForUrl(url);
	}
	
	@Override
	public int del(Object id) {
		Record record = dao.table("sys_page").where("id", id).get();
		if(record==null) {
			throw new StatusException.ApiError("页面不存在");
		}
		updateCache(record);
		
		return super.del(id);
	}
	
	
	
}
